<%@ include file="../import_page.jsp" %>
<%
	//login checking
	if(!loginedChecking(session, UsersType.staff)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	//input parameter
	int acId = -1;
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	if(isEmptyString(acIdStr)) {
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}
	
	//Convert string to int
	try {
		acId = Integer.parseInt(acIdStr);
		if(acId < 1)
			throw new Exception();
	} catch(Exception err) {
		//incorrect input, go back view assignment
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}

	
	
	String errMsg = ""; //For collector Error Message
	AssignmentCollector ac = null;
	ArrayList<AssignmentResult> listResult = null;
	//Get Assignment Detial
	try {
		AssignmentsControl assCon = new AssignmentsControl();
		ac = assCon.getAssignment(acId);
		//check the unit id is matched
		if(!ac.getUnitID().equalsIgnoreCase(((Units)session.getAttribute(SESSION_SELECT_UNIT)).getId())) {
			//if not match, go back view assignment 
			assCon.closeDBConnection();
			response.sendRedirect(URL_VIEW_ASSIGNMENT);
			return;
		}
		
		ResultControl rCon = new ResultControl();
		listResult = rCon.getResultListByAssignment(acId);
		
		assCon.closeDBConnection();
		rCon.closeDBConnection();
		
	} catch (Exception e) {
		errMsg += e.toString() + "<br />\r\n";
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Assignment Collector - Uni-Learn</title>
<style type="text/css">
<!--
.style1 {font-size: large}
-->
</style>
</head>

<body>
<h1  class="style1">Assignment Name: <%=(ac == null)? "(None)": ac.getName() %> (<%=(ac == null)? "(None)": ac.getId() %>)</h1>
<table width="100%" border="1px" bgcolor="#00FF99">
	<tr>
		<th>Student ID</th>
		<th>Name</th>
		<th>Group</th>
		<th>Submit Time</th>
		<th>File</th>
		<th>Status</th>
		<th>Mark</th>
	</tr>
<%
	if(!errMsg.isEmpty()) {
%>
	 <tr>
	 	<td colspan="7"><%=errMsg %></td>
	 </tr>
<%
	} else {
		try {
			UserControl uCon = new UserControl();
	 		Iterator<AssignmentResult> lrI = listResult.iterator();
	 		while(lrI.hasNext()) {
	 			AssignmentResult ar = lrI.next();
	 			Users aUser = uCon.getUser(ar.getSubmitBy());
	 			Hashtable<String, String> keys = new Hashtable<String, String>(2);
	 			keys.put(PAR_SUBMIT_BY, ar.getSubmitBy());
	 			keys.put(PAR_ASSIGNMENT_ID, Integer.toString(ar.getAcId()));
%>
	<tr>
		<td><%=aUser.getId() %></td>
		<td><%=aUser.getName() %></td>
		<td><%=ar.getAgId() %></td>
		<td><%=formatDateTime(ar.getSubmitTime()) %></td>
		<td><a href="<%= addParmeterToUrl(URL_DOWNLOAD_ASSIGNMENT, keys) %>"><%=ar.getFileName() %></a></td>
		<td><%=ar.getStatus().toString() %></td>
		<td><a href="<%= addParmeterToUrl(URL_VIEW_DETAIL_RESULT, keys)%>"><%=(((ar.getStatus() == SubmitStatus.marked) ? ar.getMark() : 0)+ " / "+ ac.getFullMark()) %></a> <a href="<%= addParmeterToUrl(URL_MARK_SUBMIT, keys)%>">MarkNow!</a></td>
	</tr>
<%
	 		}
	 		uCon.closeDBConnection();
		} catch (Exception e) {
			errMsg += e.toString() + "<br />\r\n";
		}
	}
	if(!errMsg.isEmpty()) {
%>
	<tr>
	 	<td colspan="7"><%=errMsg %></td>
	 </tr>
<% } %>
</table>
</body>
</html>